﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Services;
using System.Data.SqlClient;
using Telerik.Web.UI;
using BUS;
using DTO;



public partial class admin_AdminAccounts : System.Web.UI.Page
{
    AdministratorBUS adminBUS = new AdministratorBUS();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["adminName"] == null)
            Response.Redirect("login.aspx?turn=adminaccounts.aspx");
        if(!IsPostBack)
        grvAdministrators.DataSource = adminBUS.GetAllAdministrators();
        
    }

    public void btnAdd_Click(object sender, EventArgs e)
    {
        if (txtAdminName.Text == "" || txtPassword.Text == "")
            return;
        AdministratorDTO admin = new AdministratorDTO();
        admin.AdminID = txtAdminName.Text;
        admin.Password = txtPassword.Text;
        admin.RoleID = 2;
        if(adminBUS.AddAdministrator(admin))
            grvAdministrators.DataSource = adminBUS.GetAllAdministrators();
        grvAdministrators.MasterTableView.Rebind();     
    }

    
    public void grvAdministrator_OnItemDelete(object sender, GridCommandEventArgs e)
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        string id = dataItem.GetDataKeyValue("AdminID").ToString();
        adminBUS.DeleteAdministrator(id);
        grvAdministrators.DataSource = adminBUS.GetAllAdministrators();
    }

  ////  tao nguoi dung 
  //  public  void CreateUser(string username, string password, string ketnoi)
  //  {
  //      SqlDataAdapter da = new SqlDataAdapter("select * from  Administrators  where  adminID = '"+ username + "'",ketnoi );
  //      DataTable dt= new DataTable();
  //      da.Fill(dt);
  //      if(dt.Rows.Count==0)
  //      {
  //          string MaHoaPass;
  //          MaHoaPass = GetMD5(password.ToString());

  //        ///ma hoa voi thuat toan md5
  //          ketnoi.Open();
  //          cmd = new SqlCommand("sp_Admin_add",ketnoi);
  //          cmd.CommandType = CommandType.StoredProcedure;
  //          cmd.Parameters.Add("@username",SqlDbType.NVarChar).Value =  username;
  //          cmd.Parameters.Add("@password",SqlDbType.NVarChar).Value=MaHoaPass;
  //          cmd.ExecuteNonQuery();
  //          Response.Redirect("AdminAccounts.aspx");

  //      } else if((dt.Rows[0][2].ToString())==(username.ToString()))
  //      {
  //          Response.Write("<script language =\"Javascript\">");
  //          Response.Write("alert('Them khong thanh cong \\n ten dang nhap da co nguoi su dung');");
  //      }

  //  }
  //  public string GetMD5(string chuoi)
  //  {
  //      string str_md5 = "";
  //      byte[] mang = System.Text.Encoding.UTF8.GetBytes(chuoi);
  //      MD5CryptoServiceProvider my_md5 = new MD5CryptoServiceProvider();
  //      mang = my_md5.ComputeHash(mang); 
  //      foreach (byte b in mang)
  //      {
  //          str_md5 += b.ToString("X2");
  //      }
  //      return str_md5;
  //  }

}
